package com.mokylin.dbschema;

import com.mokylin.dbschema.config.ConfigInfo;
import com.mokylin.dbschema.util.DBUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author 李朝(Li.Zhao).
 * @since 2015/12/17.
 */

public class VersionTestUtils {

    static File sqlDir = new File(VersionTestUtils.class.getClassLoader().getResource("sqls").getFile());

    static{
        if(!sqlDir.exists()){
            sqlDir.mkdir();
        }
    }


    static String prefix   = "test_";

    public static void clearSqlFiles(){
        for(File f:sqlDir.listFiles()){
            f.delete();
        }
    }

    public static Connection getConn(){
        try {
            Class.forName(ConfigInfo.getJdbcDriver());
            //            connection.setAutoCommit(false);
            return DriverManager.getConnection(
                    String.format("jdbc:mysql://%1$s:%2$s/%3$s", ConfigInfo.getDbHost(), ConfigInfo.getDbPort(), ConfigInfo.getDbName()),
                    ConfigInfo.getDbUsername(),
                    ConfigInfo.getDbPassword());
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public static void dropVersionTable(){
        try {
            Connection conn = DBUtils.getConn();
            PreparedStatement preparedStatement = conn.prepareStatement("drop table t_db_version");
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void rightSqls(int length){
        rightSqls(0,length);
    }

    public static void rightSqls(int start,int length){
        clearSqlFiles();
        try {
            for(int i=start;i<=length;i++){
                File file = new File(sqlDir,prefix+i+".sql");
                String sql = "select "+i+";";
                FileWriter writer = new FileWriter(file);
                try {
                    writer.write(sql);
                } finally {
                    writer.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void invalidSqls(int length, int invalVersion) {
        try {
            clearSqlFiles();
            for(int i=0;i<=length;i++){
                File file = new File(sqlDir,prefix+i+".sql");
                String sql = "select "+i+";";
                if(i==invalVersion){
                    sql+="\n select * from aaaaaaaaa;";
                }
                FileWriter writer = new FileWriter(file);
                try {
                    writer.write(sql);
                } finally {
                    writer.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test(){
        clearSqlFiles();
    }
}
